<extend name="./Application/Admin/View/Layouts/application.html"/>

<block name="content">
    <div class="admin-content">
        <form class="am-form" action="{{:U('edit',['id'=>$product[id]])}}" method="post">

            <div class="admin-content-body">
                <div class="am-cf am-padding am-padding-bottom-0">
                    <div class="am-fl am-cf">
                        <strong class="am-text-primary am-text-lg">编辑商品</strong> /
                        <small>Edit List</small>
                    </div>
                </div>

                <hr>

                <div class="am-tabs am-margin" data-am-tabs>
                    <ul class="am-tabs-nav am-nav am-nav-tabs">
                        <li class="am-active"><a href="#tab1">基本信息</a></li>
                        <li><a href="#tab2">详细描述</a></li>
                        <li><a href="#tab3">商品相册</a></li>
                    </ul>

                    <div class="am-tabs-bd">
                        <div class="am-form  am-tab-panel am-fade am-in am-active" id="tab1">
                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">所属类别</div>
                                <div class="am-u-sm-8 am-u-md-3 am-u-end col-end">
                                    <select data-am-selected="{btnWidth: '100%',  btnStyle: 'secondary', btnSize: 'sm', maxHeight: 360, searchBox: 1}"
                                            name="category_id">
                                        <volist name="categories" id="category">
                                            <option value="{{$category.id}}" disabled>{{$category.name}}</option>
                                            <volist name="category.children" id="child">
                                                <option value="{{$child.id}}"
                                                <eq name="child.id" value="$product.category_id">selected</eq>
                                                >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|--{{$child.name}}
                                                </option>
                                            </volist>
                                        </volist>
                                    </select>
                                </div>
                            </div>

                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                    名称
                                </div>
                                <div class="am-u-sm-8 am-u-md-3 am-u-end col-end">
                                    <input type="text" required rangelength="2,20" value="{{$product.name}}" name="name"
                                           class="am-input-sm">
                                </div>
                            </div>

                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                    价格
                                </div>
                                <div class="am-u-sm-8 am-u-md-3 am-u-end col-end">
                                    <input type="text" required name="price" value="{{$product.price}}"
                                           class="am-input-sm">
                                </div>
                            </div>

                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                    库存
                                </div>
                                <div class="am-u-sm-8 am-u-md-3 am-u-end col-end">
                                    <input type="text" name="stock" value="{{$product.stock}}" class="am-input-sm">
                                </div>
                            </div>

                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                    缩略图
                                </div>
                                <div class="am-u-sm-8 am-u-md-10">
                                    <div class="upload_one am-icon-cloud-upload am-btn am-btn-default"></div>
                                    <div class="view_one">
                                        <input type="hidden" name="thumb" value="{{$product.thumb}}">
                                        <img src="{{$product.thumb}}" class="am-img-responsive"
                                             style="margin-top: 8px;max-height: 60px;">
                                    </div>
                                </div>
                            </div>

                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">是否上架</div>
                                <div class="am-u-sm-8 am-u-md-10">
                                    <label>
                                        <input type="radio" value="1" name="is_onsale"
                                        <eq name="product.is_onsale" value="1">checked</eq>
                                        > 是
                                    </label>
                                    <label>
                                        <input type="radio" value="0" name="is_onsale"
                                        <eq name="product.is_onsale" value="0">checked</eq>
                                        > 否
                                    </label>
                                </div>
                            </div>

                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                    发布时间
                                </div>
                                <div class="am-u-sm-8 am-u-md-3 am-u-end col-end">
                                    <div class="am-input-group am-datepicker-date"
                                         data-am-datepicker="{format: 'yyyy-mm-dd'}">
                                        <input type="text" name="created_at"
                                               value="{{$product.created_at|date='Y-m-d',###}}" class="am-form-field"
                                               placeholder="请输入日期"
                                               readonly>
                                        <span class="am-input-group-btn am-datepicker-add-on">
                                        <button class="am-btn am-btn-default" type="button">
                                            <span class="am-icon-calendar"></span>
                                        </button>
                                    </span>
                                    </div>
                                </div>
                            </div>

                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">推荐类型</div>
                                <div class="am-u-sm-8 am-u-md-10">
                                    <div class="am-btn-group" data-am-button>
                                        <label class="am-btn am-btn-default am-btn-xs">
                                            <input type="checkbox" name="is_top"
                                            <eq name="product.is_top" value="$product.is_top">checked</eq>
                                            > 置顶
                                        </label>
                                        <label class="am-btn am-btn-default am-btn-xs">
                                            <input type="checkbox" name="is_new"
                                            <eq name="product.is_top" value="$product.is_new">checked</eq>
                                            > 新品
                                        </label>
                                        <label class="am-btn am-btn-default am-btn-xs">
                                            <input type="checkbox" name="is_hot"
                                            <eq name="product.is_top" value="$product.is_hot">checked</eq>
                                            > 热卖
                                        </label>
                                        <label class="am-btn am-btn-default am-btn-xs">
                                            <input type="checkbox" name="is_recommend"
                                            <eq name="product.is_top" value="$product.is_recommend">checked</eq>
                                            > 推荐
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="am-form am-tab-panel am-fade" id="tab2">

                            <div class="am-g am-margin-top-sm">
                                <div class="am-u-sm-12 am-u-md-2 am-text-right admin-form-text">
                                    内容描述
                                </div>
                                <div class="am-u-sm-12 am-u-md-10">

                                    <!-- 加载编辑器的容器 -->
                                    <textarea id="container" name="description" type="text/plain">{{$product.description}}
                                    </textarea>
                                </div>
                            </div>
                        </div>

                        <div class="am-form am-tab-panel am-fade" id="tab3">
                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                    商品相册
                                </div>
                                <div class="am-u-sm-12 am-u-md-10">
                                    <div id="upload_more" class="am-icon-cloud-upload am-btn am-btn-default"></div>
                                    <div id="view_more"></div>
                                </div>

                                <volist name="galleries" id="gallery">
                                    <span class="img-box">
                                        <img class="img img-responsive" width="80" src="{{$gallery.img}}">
                                        <a class="label label-danger remove-picture" href="javascript:void(0);">删除
                                        </a>
                                        <input type="hidden" name="gallery[]" value="{{$gallery.id }}">
                                    </span>
                                </volist>

                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="am-margin">
                <button type="submit" class="am-btn am-btn-primary am-btn-xs">提交保存</button>
                <button type="reset" class="am-btn am-btn-warning am-btn-xs">放弃保存</button>
            </div>
        </form>
    </div>


    <footer class="admin-content-footer">
        <hr>
        <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
    </footer>
</block>

<block name="js">
    <script src="__PUBLIC__/vendor/bootstrapextend/js/bootstrap.extend.js"></script>

    <script charset="utf-8" src="__PUBLIC__/vendor/validate/jquery.validate.min.js"></script>
    <script charset="utf-8" src="__PUBLIC__/vendor/validate/localization/messages_zh.js"></script>

    <!-- 配置文件 -->
    <script type="text/javascript" src="__PUBLIC__/vendor/ueditor/utf8-php/ueditor.config.js"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="__PUBLIC__/vendor/ueditor/utf8-php/ueditor.all.js"></script>

    <script>
        $(function () {
            //实例化编辑器
            var ue = UE.getEditor('container', {
                initialFrameWidth: 800,//设置编辑器宽度
                initialFrameHeight: 200,//设置编辑器高度
                scaleEnabled: true,
            });

            //js自动验证
            $(".am-form").validate({
                onfocusout: function (element) {
                    $(element).valid();
                }
            });

            //上传单张图片
            $('.upload_one').Huploadify({
                uploader: '{{:U("Admin/Uploadfile/upload")}}',
                fileTypeExts: '*.gif;*.jpg;*.jpeg;*.png;*.bmp',
                //fileSizeLimit:{:C('UPLOAD_IMAGE_SIZE')}*1024,
                buttonText: '上 传 图 片',
                onUploadComplete: function (file, data) {
                    data = $.parseJSON(data);
                    $('.view_one img').attr('src', data.real_path);
                    $('.view_one input').attr('value', 'http://oze5mdef4.bkt.clouddn.com/' + data.savename);

                }
            });

            //上传多张
            $('#upload_more').Huploadify({
                uploader: '{{:U("Admin/Uploadfile/upload")}}',
                fileTypeExts: '*.gif;*.jpg;*.jpeg;*.png;*.bmp',
//                fileSizeLimit:{:C('UPLOAD_IMAGE_SIZE')}*1024,
                buttonText: '上传多张图片',
                onUploadComplete: function (file, data) {
                    var data = $.parseJSON(data);
                    //console.log(data);
                    if (data.error == 1) {
                        alertMessager(data.message);
                    } else {
                        var input = $('#view_more input');
                        var new_img = '<span class="img-box">' +
                            '<img class="img img-responsive" width="80" src="' + data.real_path + '">' +
                            '<a class="label label-danger remove-picture" href="javascript:void(0);" >删除</a>' +
                            '<input type="hidden" name="gallery[]" value="' + data.real_path + '">' +
                            '</span>';
                        $('#view_more').append(new_img);
                    }
                }
            });

            //删除图片 <i class="glyphicon glyphicon-remove-sign remove-picture"></i>
            $('#view_more').on('click', '.remove-picture', function () {
                var ready_for_remove_id = $(this).closest('.img-box').find('img').attr('data-id'); //获取待删除的图片ID
                if (!ready_for_remove_id) {
                    alertMessager('错误');
                }
                var current_picture_ids = $('#view_more input').val().split(","); //获取当前图集以逗号分隔的ID并转换成数组
                current_picture_ids.remove(ready_for_remove_id); //从数组中删除待删除的图片ID
                $('#view_more input').val(current_picture_ids.join(',')) //删除后覆盖原input的值
                $(this).closest('.img-box').remove(); //删除图片预览图
            });
        })
    </script>
</block>